//体温监测－－－－－工作人员测量体温并上报信息
var app = getApp();
var QR = require("../../../utils/qrcode.js");
var util = require('../../../utils/util.js');
var select_fz, twvalue, inf_id;
var select_page, name, sex, age, sfz, phone, sfxiaoqu_person_car, car_number, address, out_in_mesage,
  out_where, out_or_inv, fromwhere_time, fromwhere, memo;
var gzry_dep = "测试人员部门", gzry_zqdd = "测试工作地点", gzry_name = "测试工作人员", permission = false, gzryphone, fenzhenMemo, laifangmudi, beifangzhekeshi, beifangzhename, outtime, gzry_memo, downFile;
Page({

  data: {
    show: "",
    baseinformation: false,
    testmesage:true,
    xiaoqu_view: true,
    feliddown:true,
    hccc: "",
    tbry: "",
    items: [
      { name: '患者告知书', value: '患者告知书' },
      { name: '门诊分诊台', value: '门诊分诊台' },
      { name: '住院分诊台', value: '住院分诊台' },
    ],
    modalHidden: true,//是否隐藏对话框
    xiaoqu_gzrrzqdd: false,
    xiaoqu_tw_moda: true,
    imgalist: ['https://7a6d-zmyy-6uke0-1301213777.tcb.qcloud.la/%E6%B5%8B%E8%AF%95%E5%A1%AB%E6%8A%A5%E4%BF%A1%E6%81%AF%E4%BA%8C%E7%BB%B4%E7%A0%81.png?sign=7f701e4fdc630007bc45ef91445cc202&t=1581432429']
  },
  
 
  

  /**
     * 生命周期函数--监听页面显示
     */
  onShow: function () {
    // this.findOpenid()
  },
  onLoad: function () {
    
    this.setData({
      zmyygzs_zqdd_moda: false,
    })
  }, 
 
  //获取临时缓存照片路径，存入data中

  
  
  testdown: function (e) {
    // console.log('没e有人员信息', downFile)
    let that = this;
    
      wx.showToast({
        title: '上传文件请等待',
        icon: 'success',
      })
      //读取users表数据
      wx.cloud.callFunction({
        name: "test_getdata",///////////////传参数
        // name: "xiaoqu_jinghunanyuan_getdata",
        success(res) {
          // console.log("读取成功cloud_data", cloud_data )
          that.savaExcel(res.result.data)
        },
        fail(res) {
          console.log("读取失败getusers", res)
        }
      })
    
    

  }, 
  // 把数据保存到excel里，并把excel保存到云存储
  savaExcel(userdata) {
    let that = this
    wx.cloud.callFunction({
      name: "test_getexcel",
      data: {
        userdata: userdata
      },
      success(res) {
        console.log("保存成功", res)
        that.getFileUrl(res.result.fileID)
      },
      fail(res) {
        console.log("保存失败", res)
      }
    })
  },
  ///////////////////////////以下公用函数
  //获取云存储文件下载地址，这个地址有效期一天
  getFileUrl(fileID) {
    let that = this;
    wx.cloud.getTempFileURL({
      fileList: [fileID],
      success: res => {
        // get temp file URL
        console.log("文件下载链接", res.fileList[0].tempFileURL)
        that.setData({
          fileUrl: res.fileList[0].tempFileURL,
          xdmy:"吓到没有………………",
          fielddf:"　　复制链接后把链接发送到电脑上下载",
          feliddown: false
        })
      },
      fail: err => {
        // handle error
      }
    })
  },
  //复制excel文件下载链接
  copyFileUrl() {
    let that = this
    wx.setClipboardData({
      data: that.data.fileUrl,
      success(res) {
        wx.getClipboardData({
          success(res) {
            console.log("复制成功", res.data) // data
          }
        })
      }
    })
  },
  scan_info_return: function (e) {
    select_page = "重点人员信息上报"
   
    
      this.scanCode();
    
  },

  scan_info_out: function (e) {
    select_page = "人员入出登记"
   this.scanCode();
   
  },



  findOpenid: function () {
    var that = this;
    const db = wx.cloud.database();
    wx.cloud.callFunction({
      name: 'login',
      complete: res => {
        // console.log('onGetOpenid result: ', app.globalData.openid)
        app.globalData.openid = res.result.openid

        db.collection('glry').where({
          _openid: app.globalData.openid
        })
          .get()
          .then(res => {
            // console.log('马上查询数据库，opneid为:', app.globalData.openid)
            if (res.data.length) {
              // wx.showToast({
              //   title: '已添加过'
              // })
              // console.log('已添加过', res.data)
              // console.log('已添加过', res.data[0]._id)
              this.setData({
                tbry: "填报人员为 " + res.data[0].dep + " 的: " + res.data[0].name,
                gzry_zqdd: res.data[0].zqaddress,
                gzry_name: res.data[0].name,
                // gzry_name: res.data[0].姓名,
                // name_phone: res.data[0].电话,               
              })
              // personid = res.data[0]._id
              gzry_dep = res.data[0].dep
              gzry_zqdd = res.data[0].zqaddress
              gzry_name = res.data[0].name
              gzryphone = res.data[0].gzryphone
              permission = res.data[0].ruler
              // name_phone = res.data[0].电话
            }
            else {
              // console.log('没e有人员信息', res.data)
              // console.log('this.data.modalHidden1', this.data.modalHidden1)
              this.setData({
                tbry: "请到 服务中心-->工作人员绑定 后再使用扫码功能！"
              })
            }
          })
      }
    })


  },
  //事件处理函数
  // bindViewTap: function () {
  //   this.setData({
  //     modalHidden: !this.data.modalHidden
  //   })

  // },


  //确定按钮点击事件,确认值勤信息
  xiaoqu_zqdd_en: function () {
    this.setData({
      xiaoqu_gzrrzqdd: true
    })
  },
  //取消按钮点击事件,确认值勤信息
  xiaoqu_zqdd_ca: function () {
    wx.showToast({
      title: '修改完成',
      icon: 'success',
      duration: 2000
    })
    this.setData({
      xiaoqu_gzrrzqdd: true
    })
  },
  //确定按钮点击事件,体温
  xiaoqu_tw_en: function () {
    // console.log('确定按钮点击事件,门诊体温') 
    this.add_xiaoqu();
    this.setData({
      xiaoqu_tw_moda: true,
     
    })
   
  },
  //取消按钮点击事件,体温
  xiaoqu_tw_ca: function () {
    // console.log('取消按钮点击事件,门诊体温：') 
    // wx.navigateTo({
    //   url: '/pages/gzryinfo/gzryinfo'
    // })
    this.setData({
      xiaoqu_tw_moda: true
    })
  },
  test: function () {
    // console.log("二维码成功222：", name1, sex1, sfz1)
  },
  scanCode: function (e) {

    const db = wx.cloud.database();
    wx.scanCode({
      // onlyFromCamera: true,////只允许从相机扫码，去掉可加载相册图片
      success: (res) => {
        // this.show = "结果:" + res.result + "二维码类型:" + res.scanType + "字符集:" + res.charSet + "路径:" + res.path;
        // app.globalData.TwoCode = false
        inf_id = res.result
        db.collection('base_test').doc(res.result).get(
          // console.log("二维码成功,id：", res.result)
        ).then(res => {
          // res.data 包含该记录的数据
          // console.log("二维码成功,携带数据：", res.data)
          this.setData({
            returename: "上次复核的人员为：" + res.data.name,//提交后提示上次填加人员
            xiaoqu_view: false,
            baseinformation: true,
            testmesage: true,
            name: res.data.name,
            sex: res.data.sex,
            age: res.data.age,
            sfz: res.data.sfz,
            phone: res.data.phone,
            sfxiaoqu_person_car: res.data.sfxiaoqu_person_car,
            car_number: res.data.car_number,
            address: res.data.address,
            out_in_mesage: res.data.out_in_mesage,
            out_where: res.data.out_where,
            out_or_inv: res.data.out_or_inv,
            fromwhere_time: res.data.fromwhere_time,
            fromwhere: res.data.fromwhere,
            memo: res.data.memo


          })

          name = res.data.name,
            sex = res.data.sex,
            age = res.data.age,
            sfz = res.data.sfz,
            phone = res.data.phone,
            sfxiaoqu_person_car = res.data.sfxiaoqu_person_car,
            car_number = res.data.car_number,
            address = res.data.address,
            out_in_mesage = res.data.out_in_mesage,
            out_where = res.data.out_where,
            out_or_inv = res.data.out_or_inv,
            fromwhere_time = res.data.fromwhere_time,
            fromwhere = res.data.fromwhere,
            memo = res.data.memo

          // // console.log("门诊台选择2：", select_fz)



        })
          .catch(Error => {
            // console.log("二维码失败wyth：")
            wx.showToast({
              title: '无效信息码',
              icon: 'success',
              duration: 2000
            })
          })



      },
      fail: (res) => {
        // console.log("二维码扫描结果5：", res)
        wx.showToast({
          title: '失败',
          icon: 'success',
          duration: 2000
        })
      },
      complete: (res) => {
        // console.log("二维码扫描失败：", TwoCode )
      }

    })



  },


  xiaoqu_twvalue_check: function (e) {
    if (parseFloat(twvalue) > 34 && parseFloat(twvalue) < 41) {
      // console.log("输出1：", parseFloat(twvalue))
      if (parseFloat(twvalue) >= 37.3) {
        // console.log("wv分诊选择", select_fz)

        this.setData({
          xiaoqu_tw_moda: false
          // inputVal: "",
        })

      }
      else {
        // console.log("体温正常", parseFloat(twvalue))
        this.add_xiaoqu();
        
      }
    }
    else {
      // console.log("不在范围", parseFloat(twvalue))
      wx.showToast({
        title: '体温不对请重测',
        icon: 'success'
      })
    }
    
  },
 
  add_xiaoqu: function (e) {
    const db = wx.cloud.database()
    this.setData({
      xiaoqu_view: true,
      baseinformation: false,   ////扫描页面
      testmesage: false,
      inputVal: "",     //体温 输入 框
      gzry_memo: "",
    })
    // console.log("yn康记录ID1111111", gzry_memo)
    db.collection('info_test').add({
      // data 传入需要局部更新的数据

      data: {

        // 表示将 done 字段置为 true 
        name: name,
        sex: sex,
        age: age,
        sfz: sfz,
        phone: phone,
        sfxiaoqu_person_car: sfxiaoqu_person_car,
        car_number: car_number,
        address: address,
        out_in_mesage: out_in_mesage,
        out_where: out_where,
        out_or_inv: out_or_inv,
        fromwhere_time: fromwhere_time,
        fromwhere: fromwhere,
        memo: memo,
        form: "小区测温",
        gzryphone: gzryphone,
        gzry_addtime: util.formatTime(new Date()),
        gz_address: gzry_zqdd,
        gzry_dep: gzry_dep,
        gzry_name: gzry_name,
        twvalue: twvalue,
        gzry_memo: gzry_memo,
        select_page: select_page

      },
      success: function (res) {
       
        // console.log("yn康记录I33333333333333", gzry_memo)
        wx.showToast({
          title: '人员上报成功',
          icon: 'success',
        })
      }
    })
    twvalue = ""
    gzry_memo = ""

  }
  ,


  onInputEvent: function (e) {
    twvalue = e.detail.value
    this.setData({
      returetwvalue: "  体温：" + twvalue,
      twvalue_test: e.detail.value
    })
  },
  memo_Event: function (e) {
    gzry_memo = e.detail.value
    // console.log("yn康记录ID", gzry_memo)
    // this.setData({
    //   gzry_memo: "  体温：" + twvalue
    // })
  },



})
